<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="充电桩编码" label-width="100px" prop="stationCode">
        <el-input
          v-model="queryParams.stationCode"
          placeholder="请输入充电桩编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="充电场所" prop="field101">
        <el-select
          v-model="queryParams.areaId"
          filterable
          placeholder="请选择充电场所"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in areaOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="充电桩状态" label-width="100px" prop="field101">
        <el-select
          v-model="queryParams.state"
          placeholder="请选择充电桩状态"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in stateOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="stationList"
      @selection-change="handleSelectionChange"
      size="small"
    >
      <el-table-column label="充电桩id" align="center" prop="stationId" />
      <el-table-column label="充电桩编码" align="center" prop="stationCode" />
      <el-table-column label="充电场所" align="center" prop="areaName" />
      <el-table-column label="充电桩状态" align="center" prop="state" />
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { serverIp } from "../../public/config";
import { listArea } from "@/api/area";
import {
  listStation,
  getStation,
  delStation,
  addStation,
  updateStation,
} from "@/api/station";

export default {
  name: "Station",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充电桩表格数据
      stationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationCode: null,
        areaId: null,
        state: null,
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 50,
      },
      areaOptions: [],
      stateOptions: [
        {
          label: "充电中",
          value: "充电中",
        },
        {
          label: "空闲",
          value: "空闲",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询充电场所列表 */
    /** 查询充电桩列表 */
    getList() {
      listArea(this.queryParams2).then((response) => {
        const list = response.data.records.map((e) => {
          const obj = {
            value: e.areaId,
            label: e.areaName,
          };
          return obj;
        });
        console.log("list----", list);

        this.areaOptions = list;
      });
      this.loading = true;
      listStation(this.queryParams).then((response) => {
        this.stationList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        stationId: null,
        stationCode: null,
        areaId: null,
        state: null,
        remark: null,
      };
    //   this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        stationCode: null,
        areaId: null,
        state: null,
      }
    //   this.resetForm("queryForm"); 
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.stationId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
  },
};
</script>
